﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Devtm.ServiceModel;

namespace Devtm.ServiceModel.Proxy
{
    

    internal class ManagerFactory
    {


        private static Hashtable _dic = new Hashtable();


        internal static ChannelFactoryManager<T> Get<T>(Func<ChannelFactoryManager<T>> manager, params string[] keys)
        {
            
            StringBuilder s = new StringBuilder(typeof(T).AssemblyQualifiedName);
            foreach (string k in keys)
                s.Append(k);

            uint key = Devtm.ServiceModel.Helpers.Crc32.Calculate(s.ToString());

            ChannelFactoryManager<T> value;

            if (!_dic.ContainsKey(key))
            {

                lock (_dic.SyncRoot)
                {

                    if (!_dic.ContainsKey(key))
                    {
                        value = manager();
                        _dic.Add(key, value);
                    }
                    else                    
                        value = _dic[key] as ChannelFactoryManager<T>;
                    
                }

            }
            else            
                value = _dic[key] as ChannelFactoryManager<T>;            

            return value;

        }

    }
}
